//
//  Config.cpp
//  app-ios gl
//
//  Created by MeherTj on 2022/1/22.
//  Copyright © 2022 lover. All rights reserved.
//

#include "Config.hpp"

Zen::Random g_random;

static Pos s_tetris_pos_config[GDirectCount][TetrisTypeCount][GPotsMaxCount] = {
	{
		{ { 0, 0, }, { 0, -1 }, { -1, -1 }, { -1, 0 } },
		{ { 0, 0, }, { 1, 0 }, { -1, -1 }, { -1, 0 } },
		{ { 0, 0, }, { 1, 0 }, { 1, -1 }, { -1, 0 } },
		{ { 0, 0, }, { 1, -1 }, { 0, -1 }, { -1, 0 } },
		{ { 0, 0, }, { 1, 0 }, { 0, -1 }, { -1, -1 } },
		{ { 0, 0, }, { 1, 0 }, { -2, 0 }, { -1, 0 } },
		{ { 0, 0, }, { 1, 0 }, { 0, -1 }, { -1, 0 } },
		
		{ { 0, 0, }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
		{ { 0, 0, }, { 1, 0 }, { 0, 0 }, { 0, 0 } },
		{ { 0, 0, }, { 1, 0 }, { -1, 0 }, { 0, 0 } },

	}
};

static int s_tetris_direct_config[TetrisTypeCount] = {
	1, 4, 4, 2, 2, 2, 4,
	1, 2, 2
};

static int s_tetris_pots_count[TetrisTypeCount] = {
	4, 4, 4, 4, 4, 4, 4,
	1, 2, 3,
};

static bool s_pos_inited = false;

static void init_pos_config()
{
	auto & src = s_tetris_pos_config[0];
	auto & d1 = s_tetris_pos_config[1];
	auto & d2 = s_tetris_pos_config[2];
	auto & d3 = s_tetris_pos_config[3];

	for(int i = 1; i < TetrisTypeCount; ++i)
	{
		auto pc = s_tetris_pots_count[i];
		auto d = s_tetris_direct_config[i];

		for(int j = 0; j < pc; ++j)
		{
			auto const & t = src[i][j];
			if(d > 1) d1[i][j] = { t.y, -t.x };
			if(d > 2) d2[i][j] = { -t.x, -t.y };
			if(d > 3) d3[i][j] = { -t.y, t.x };
		}
	}

}

TetrisConfig::TetrisConfig()
{
	if(!s_pos_inited) init_pos_config();
	random.reset((int)time(0));
}

int TetrisConfig::GetDirectCount(enum TetrisType type) {
	return s_tetris_direct_config[type];
}
std::vector<Pos> TetrisConfig::GetSubPoses(enum TetrisType type, int direct, Pos pos) {
	std::vector<Pos> ps;
	direct %= GetDirectCount(type);
	auto & c = s_tetris_pos_config[direct][type];
	for(int i = 0; i < s_tetris_pots_count[type]; ++i) {
		Pos p;
		p.x = pos.x + c[i].x;
		p.y = pos.y + c[i].y;
		ps.push_back(p);
	}
	return ps;
}
TetrisInfo TetrisConfig::getNext()
{
	TetrisInfo ti;
	ti.type = (enum TetrisType)(random.next(m_use_special? TetrisTypeCount : TetrisTypeNormalCount));
	ti.direct = int(random.next(GDirectCount)) % GetDirectCount(ti.type);
	ti.color = (int)(random.next(GBlockColorCount) + 1);
	return ti;
}
